package cn.yfj.pet.service.impl;

import cn.yfj.basic.util.BaiduAuditUtils;
import cn.yfj.basic.util.DistanceUtil;
import cn.yfj.basic.util.Point;
import cn.yfj.org.domain.Shop;
import cn.yfj.org.mapper.ShopMapper;
import cn.yfj.pet.domain.SearchMasterMsg;
import cn.yfj.pet.domain.SearchMasterMsgAuditLog;
import cn.yfj.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.yfj.pet.mapper.SearchMasterMsgMapper;
import cn.yfj.pet.service.ISearchMasterMsgService;
import cn.yfj.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yfj
 * @since 2022-02-27
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Override
    @Transactional
    public void publish(SearchMasterMsg msg) {
        //保存
        searchMasterMsgMapper.save(msg);
        //开启百度Ai审核
        //拼接需要审核的内容
        String auditText = msg.getTitle() + msg.getAddress() + msg.getName() + msg.getCoatColor();
        StringBuilder note = new StringBuilder();
        //if(!BaiduAuditUtils.TextCensor(auditText)){
            //不合法  一个宠物信息不合法，全部上架失败吗？ 不是，只是当前记录上架失败，其他合法的要成功
          //  note.append("输入的信息不合法：名称，地址，毛色等！！");
        //}
        //审核resources
        /*String resources = msg.getResources();
        if(!StringUtils.isEmpty(resources)){
            String[] split = resources.split(",");
            if(!BaiduAuditUtils.ImgCensor("http://47.95.117.210:8888"+split[0])){
                //3.不合法  一个宠物信息不合法，全部上架失败吗？ 不是，只是当前记录上架失败，其他合法的要成功
                note.append("宠物图片不合法！");
            }
        }*/
        //审核不通过的情况下即Note不为空
        if (!StringUtils.isEmpty(note.toString())){
            msg.setState(-1); //设置为驳回状态
            //保存失败的日志
            SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
            searchMasterMsgAuditLog.setAuditTime(new Date());
            searchMasterMsgAuditLog.setMsgId(msg.getId());
            searchMasterMsgAuditLog.setNote(note.toString());
            searchMasterMsgAuditLog.setState(0);
            searchMasterMsgAuditLogMapper.save(searchMasterMsgAuditLog);
        }else {//审核通过的情况下
            msg.setState(1); //设置审核状态为通过
            //查找50公里内的店铺
            Point point = DistanceUtil.getPoint(msg.getAddress());
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
            if (shopMapper!=null){ //如果找到匹配店铺
                msg.setShopId(nearestShop.getId());
                //SmsUtils.sendSmsMsg(nearestShop.getTel(),"您有消息未读："+msg.getTitle()+";你有订单");
            }
        }
        searchMasterMsgMapper.update(msg); //如果找不到
    }
}
